package com.bwei.topnews.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bwei.topnews.R;
import com.bwei.topnews.adapter.SlidingAdapter;
import com.bwei.topnews.utils.ImageLoader_SlidingMenu;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;


/**
 * Created by qwe on 2016/11/2.
 */
public class MenuFragment extends Fragment implements View.OnClickListener{
    private ListView listView;
    private ImageButton imagephone;
    private ImageButton imageqq;
    private ImageButton imagesina;
    private LinearLayout linearLayout;
    private ImageView person_icon;
    private TextView person_name;
    private DisplayImageOptions options;
    private LinearLayout out_linearLayout;
    private ImageView image_user;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.menu_layout,null);
        listView = (ListView) view.findViewById(R.id.lv_leftdrawer);
        imagephone = (ImageButton) view.findViewById(R.id.ib_leftdraw_cellphone);
        imageqq = (ImageButton) view.findViewById(R.id.ib_leftdraw_qq);
        imagesina = (ImageButton) view.findViewById(R.id.ib_leftdraw_sina);
        linearLayout = (LinearLayout) view.findViewById(R.id.ll_leftdrawer_login_success);
        person_icon = (ImageView) view.findViewById(R.id.iv_person_icon);
        person_name = (TextView) view.findViewById(R.id.tv_person_name);
        out_linearLayout = (LinearLayout) view.findViewById(R.id.ll_leftdrawer_login);
        image_user = (ImageView) getActivity().findViewById(R.id.ImageUser);
        imagephone.setOnClickListener(this);
        imageqq.setOnClickListener(this);
        imagesina.setOnClickListener(this);
        return view;
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        List<Integer> list = new ArrayList<>();
        list.add(R.mipmap.dynamicicon_leftdrawer_night);
        list.add(R.mipmap.topicicon_leftdrawer_night);
        list.add(R.mipmap.love_jokebar_textpage_selected_normal);
        list.add(R.mipmap.activityicon_leftdrawer);
        list.add(R.mipmap.sellicon_leftdrawer);
        list.add(R.mipmap.feedbackicon_leftdrawer);
        list.add(R.mipmap.baoliao);

        listView.setAdapter(new SlidingAdapter(list,getActivity()));

        options = ImageLoader_SlidingMenu.getOptions();

    }

    @Override
    public void onClick(View v) {
        Toast.makeText(getActivity(), "as4d56as4d56", Toast.LENGTH_SHORT).show();
        switch (v.getId()){
            case R.id.ib_leftdraw_cellphone:
                RegisterPage registerPage = new RegisterPage();
                registerPage.setRegisterCallback(new EventHandler() {
                    public void afterEvent(int event, int result, Object data) {
// 解析注册结果
                        if (result == SMSSDK.RESULT_COMPLETE) {
                            @SuppressWarnings("unchecked")
                            HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
                            String country = (String) phoneMap.get("country");
                            String phone = (String) phoneMap.get("phone");

// 提交用户信息（此方法可以不调用）
                          //  registerUser(country, phone);
                        }
                    }
                });
                registerPage.show(getActivity());

                break;
            case R.id.ib_leftdraw_qq:
                UMShareAPI  mShareAPI = UMShareAPI.get(getActivity() );
                mShareAPI.getPlatformInfo(getActivity(), SHARE_MEDIA.QQ, umAuthListener);
                break;
            case R.id.ib_leftdraw_sina:
                break;

        }

    }


    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Toast.makeText(getActivity(), "Authorize succeed", Toast.LENGTH_SHORT).show();
            linearLayout.setVisibility(View.VISIBLE);
            out_linearLayout.setVisibility(View.INVISIBLE);
            String image_url = data.get("profile_image_url");
            String screen_name = data.get("screen_name");
            ImageLoader.getInstance().displayImage(image_url,person_icon,options);
            ImageLoader.getInstance().displayImage(image_url,image_user,options);
            person_name.setText(screen_name);
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getActivity(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(getActivity(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };
}
